﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Net.Http;
using System.Net.Http.Headers;
using XclusiveClient.Classes;

namespace XclusiveClient
{
    public partial class Default : System.Web.UI.Page
    {
        ArrayList clientList = new ArrayList();
        protected void Page_Load(object sender, EventArgs e)
        {
            lblStatus.Visible = false;
            lbl_login_error.Visible = false;

            //Getting all the Clients
            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri("http://localhost:3676/");
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            HttpResponseMessage response = client.GetAsync("api/Client").Result;
            if (response.IsSuccessStatusCode)
            {
                var clients = response.Content.ReadAsAsync<IEnumerable<Client>>().Result;
                foreach (var cs in clients)
                {
                    Client c = new Client();
                    c.username = cs.username;
                    c.full_name = cs.full_name;
                    c.password = cs.password;
                    c.email = cs.email;
                    c.address = cs.address;
                    c.contact_number = cs.contact_number;
                    c.company_country = cs.company_country;
                    c.company_name = cs.company_name;
                    c.company_email = cs.company_email;
                    c.company_address = cs.company_address;
                    c.account_type = cs.account_type;
                    clientList.Add(c);
                }
            }
            else
            {
                lblStatus.Visible = true;
                lblStatus.Text = "Could not login into account. Error code: " + response.StatusCode + ", Reason: " + response.ReasonPhrase.ToString() + "<br/>";
            }
        }

        protected void signupBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("Registration.aspx");
        }

        protected void loginBtn_Click(object sender, EventArgs e)
        {
            foreach (Client x in clientList)
            {
                if (x.username == usernameTbx.Text && x.password == passwordTbx.Text)
                {
                    if (x.account_type == "Administrative")
                    {
                        Session["username"] = x.username;
                        Session["company_name"] = x.company_name;
                        Response.Redirect("~/Dashboard.aspx");
                    }
                    else if (x.account_type == "Basic")
                    {
                        Session["username"] = x.username;
                        Response.Redirect("~/Dashboard.aspx"); //Later link to the website that can only view...
                    }
                }
                else
                {
                    lbl_login_error.Visible = true;
                }
            }
        }
    }
}